package cn.tedu.boyou.search.service.impl;

import cn.tedu.boyou.search.repository.ISaleTagTemplateRepository;
import cn.tedu.boyou.search.service.ISaleTagSearchService;
import cn.tedu.boyou.search.template.SaleTagTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class SaleTagSearchServiceImpl implements ISaleTagSearchService {

    @Autowired
    private ISaleTagTemplateRepository dao;

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private TransportClient client;

    public void createIndex() {
        template.createIndex(SaleTagTemplate.class);
        template.putMapping(SaleTagTemplate.class);
    }

    public void save(SaleTagTemplate t) {
        dao.save(t);
    }

    @Override
    public SaleTagTemplate querySaleTagByName(String name) {
        Iterable<SaleTagTemplate> dests = dao.search(QueryBuilders.termQuery("name", name));

        List<SaleTagTemplate> list = new ArrayList<>();
        dests.forEach(d ->{
            list.add(d);
        });
        if(list.size() > 0){
            return list.get(0);
        }
        return null;
    }

}
